/**
 * 
 */
package gui.text;

import javax.swing.Action;
import javax.swing.text.Element;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.TextAction;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;

/**
 * Editor kit for properly formatted Mathematical equations.
 * 
 * @author Joshua Lockerman
 * 
 */
public class MathKit extends StyledEditorKit {
    
    private MathViewFactory mathViewFactory;
    
    /**
     * 
     */
    private static final long serialVersionUID = -7937358236354938579L;
    
    private static final Action[] defaultActions = {};
    
    /*
     * (non-Javadoc)
     * 
     * @see javax.swing.text.EditorKit#getContentType()
     */
    @Override
    public String getContentType()
    {
        return "application/x-math";
    }
    
    @Override
    public Action[] getActions()
    {
        return TextAction.augmentList(super.getActions(), this.defaultActions);
    }
    
    @Override
    public ViewFactory getViewFactory()
    {
        if(mathViewFactory == null) {
            mathViewFactory = new MathViewFactory();
        }
        return mathViewFactory;
    }
    
    private ViewFactory superGetViewFactory()
    {
        return super.getViewFactory();
    }
    
    private class MathViewFactory implements ViewFactory {
        private final ViewFactory viewFactory;
        
        public MathViewFactory()
        {
            viewFactory = superGetViewFactory();
        }
        
        @Override
        public View create(Element elem)
        {
            /*if(elem instanceof LeafElement) {
                return new LabelView(elem);
            }
            if(elem instanceof LineElement) {
                return new ParagraphView(elem);
            }
            if(elem instanceof RootElement) {
                return new BoxView(elem, View.Y_AXIS);
            }
            if(elem instanceof BranchElement) {
                return new ParagraphView(elem);
            }*/
            /*if(elem != null && elem.getName().equals(AbstractDocument.ContentElementName)) {
                return new EquationView(elem);
            }*/
            return viewFactory.create(elem);
        }
    }
    
}
